package com.lanou.dllo.baisibudejie.activity;

import android.app.Activity;
import android.os.Bundle;

import com.lanou.dllo.baisibudejie.R;
import com.lanou.dllo.baisibudejie.volley.DiskCache;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by dllo on 15/11/4.
 */
public class SendImageActivity extends Activity implements View.OnClickListener {
    private EditText sendImage_contentEt;
    private TextView sendImage_cancelTv, sendimage_sendTv;
    private Bitmap bitmap;
    private Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置布局文件
        setContentView(R.layout.activity_sendimage);

        Intent intent = new Intent();
                /* 开启Pictures画面Type设定为image */
        intent.setType("image/*");
                /* 使用Intent.ACTION_GET_CONTENT这个Action */
        intent.setAction(Intent.ACTION_GET_CONTENT);
                /* 取得相片后返回本画面 */
        startActivityForResult(intent, 1);
        initView();
    }

    private void initView() {
        //内容
        sendImage_contentEt = (EditText) findViewById(R.id.sendImage_contentEt);
        sendImage_cancelTv = (TextView) findViewById(R.id.sendImage_cancelTv);//取消
        sendImage_cancelTv.setOnClickListener(this);
        sendimage_sendTv = (TextView) findViewById(R.id.sendimage_sendTv);//发送
        sendimage_sendTv.setOnClickListener(this);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            uri = data.getData();
            Log.e("uri", uri.toString());
            ContentResolver cr = this.getContentResolver();
            try {
//                BitmapFactory.Options opts = new BitmapFactory.Options();
//                opts.inSampleSize = 4;
//                bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri),null,opts);
                bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
                DiskCache cache = new DiskCache();
                cache.putBitmap(uri + "", bitmap);//把图片保存到本地
                ImageView imageView = (ImageView) findViewById(R.id.sendimageIv);
                /* 将Bitmap设定到ImageView */
                imageView.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                Log.e("Exception", e.getMessage(), e);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sendImage_cancelTv://返回
                finish();
                break;
            case R.id.sendimage_sendTv://发送
                String content = sendImage_contentEt.getText().toString();
                Intent intent_send = new Intent(this, MyCardActivity.class);
                intent_send.putExtra("text", content);
                intent_send.putExtra("bitmap", uri + "");
                startActivity(intent_send);
                finish();
                break;
        }
    }
}
